package com.xinxin.facelinker.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.adapter.SearchUserAdapter;
import com.xinxin.facelinker.domain.ReceiveNewPals;
import com.xinxin.facelinker.domain.SearchUser;
import com.xinxin.facelinker.utils.NetHelper;

import java.util.ArrayList;
import java.util.List;

public class SearchUserActivity extends Activity {

    private ListView lvSearchUsers;
    private String my_account_num;
    private HttpUtils httpUtils = new HttpUtils();
    private View btnBack;
    private SearchView searchView;
    private List<SearchUser> list = new ArrayList<SearchUser>();
    private SearchUserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_user);

        my_account_num = Config.getCachedAccountNum(SearchUserActivity.this);
        lvSearchUsers = (ListView) findViewById(R.id.lvSearchUsers);
        btnBack = findViewById(R.id.btnBack);
        searchView = (SearchView) findViewById(R.id.searchView);
        searchView.setIconifiedByDefault(false);
        searchView.setSubmitButtonEnabled(true);
        searchView.setQueryHint("搜索");
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                httpMethod(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                httpMethod(newText);
                return true;
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    final public void httpMethod(String searchText) {
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_SEARCH_USER);
        params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
        params.addBodyParameter(Config.KEY_SEARCH_USER_ACCOUNT_NUM, searchText);
        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson = new Gson();
                list = gson.fromJson(responseInfo.result, new TypeToken<List<SearchUser>>() {
                }.getType());

                if (list == null) {
                    Toast.makeText(SearchUserActivity.this, R.string.search_users_is_empty, Toast.LENGTH_SHORT).show();
                } else {
                    adapter=new SearchUserAdapter(list,SearchUserActivity.this);
                    lvSearchUsers.setAdapter(adapter);
                }
                //列表点击弹出好友信息
                lvSearchUsers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(SearchUserActivity.this, GetPalInfoActivity.class);
                        intent.putExtra(Config.KEY_OTHER_ACCOUNT_NUM, list.get(position).getAccount_num());
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(SearchUserActivity.this, R.string.search_users_fail, Toast.LENGTH_SHORT).show();
            }
        });
    }
}